package com.tarena.lbs.basic.web.repository;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.basic.web.mapper.BusinessMapper;
import com.tarena.lbs.pojo.basic.po.BusinessPO;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;

    public Long businessTotal(BusinessQuery query){
        //业务条件拼接 不是只有total查询使用
        QueryWrapper<BusinessPO> queryWrapper=generateQuery(query);
        return businessMapper.selectCount(queryWrapper);
    }

    private QueryWrapper<BusinessPO> generateQuery(BusinessQuery query){
        //根据非空 拼接queryWrapper
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getBusinessName())){
            log.info("businessName不为空");//business_name like %#{}%
            queryWrapper.like("business_name",query.getBusinessName());
        }
        if (StringUtils.isNoneEmpty(query.getBusinessHeadPhone())){
            log.info("businessHeadPhone不为空");//business_head_phone=#{}
            queryWrapper.eq("business_head_phone",query.getBusinessHeadPhone());
        }
        if (query.getBusinessStatus()!=null){
            log.info("businessStatus不为空");
            queryWrapper.eq("business_status",query.getBusinessStatus());
        }
        //只要star和end都不为空 才能拼接时间段查询
        if(query.getStartingTime()!=null&& query.getEndTime() != null){
            //entry_time>startTime and entry_time<endTime
            queryWrapper.between("entry_time",query.getStartingTime(),query.getEndTime());
        }
        //商家分页查询 数据权限条件 businessId不为空拼接
        if (query.getBusinessId()!=null){
            queryWrapper.eq("id",query.getBusinessId());
        }
        return  queryWrapper;
    }

    public List<BusinessPO> businessPage(BusinessQuery query){
        //业务条件 拼接 分页条件 limit 计算start rows
        // select * from lbs_business where 条件 limit start rows
        QueryWrapper<BusinessPO> queryWrapper=generateQuery(query);
        int start = (query.getPageNo()-1)*query.getPageSize();
        queryWrapper.last("limit "+start+","+query.getPageSize());
        return businessMapper.selectList(queryWrapper);
    }
    public PageInfo<BusinessPO> businessPageHelper(BusinessQuery query){
        //1、pageHelper开启分页查询
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
       /* if (1==0){
            //为了保证线程中threadlocal数据没有冗余 先清空
            PageHelper.clearPage();
            return null;
        }*/
        //2、封装业务条件查询
        QueryWrapper<BusinessPO> queryWrapper=generateQuery(query);
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        //3、第三步；pageInfo 获取所有查询结果 包括 total list pageNum pageSize
        PageInfo<BusinessPO> pageInfo = new PageInfo<>(pos);
        return pageInfo;
    }

    //2/
    public BusinessPO getBusinessByName(String businessName) {
        //select * from lbs_business where business_name=#{businessName}
        //构造查询条件 business_name=#{businessName}
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_name",businessName);
        return businessMapper.selectOne(queryWrapper);
    }

    //5/
    public void save(BusinessPO poParam) {
        businessMapper.insert(poParam);
    }


    public BusinessPO getBusiness(Integer businessId) {
        return businessMapper.selectById(businessId);
    }
}
